package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseVo;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.model.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 小兰
 * @Date: 2023/4/26 18:39
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private SearchService searchService;

    /**
     * 创建索引库
     * @return
     */
    @GetMapping("inner/createIndex")
    public Result createIndex(){
        //调用工具类创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        //设置索引库映射信息
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    @GetMapping("inner/deleteIndex")
    public  Result deleteIndex(){
        elasticsearchRestTemplate.deleteIndex("goods");
        return Result.ok();
    }

    /**
     *    todo 测试接口 将指定商品skuid封装为索引库商品文档对象,将商品存入索引库
     * @param skuId
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * 测试接口，商品文档删除
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId")Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();

    }

    /**
     * 提供给详情服务调用:更新商品热度分值
     * @param skuId
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public void  incrHotScore(@PathVariable("skuId")Long skuId){
        searchService.incrHotScore(skuId);
    }

    /**
     * 商品检索功能!
     * @param searchParam
     * @return
     */
    @PostMapping({"","/"})
    public Result search(@RequestBody SearchParam searchParam){
        SearchResponseVo searchResponseVo= searchService.search(searchParam);
        return Result.ok(searchResponseVo);
    }

}
